package com.atguigu.flink.datastream.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

/**
 * @author WEIYUNHUI
 * @date 2023/6/12 15:50
 *
 * Source算子:
 *    旧的: env.addSource(SourceFunction)
 *    新的: env.fromSource(Source , WatermarkStrategy , name)
 *
 * Source源:
 *    从集合读取数据
 *    从socket读取数据
 *    数据生成器
 *
 *    从文件读取数据
 *    从kafka读取数据
 *    自定义Source
 *
 */
public class Flink01_BaseSource {
    public static void main(String[] args) {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        //从集合中读取数据
        List<Integer> lists = Arrays.asList(1, 2, 3, 4, 5);
        //DataStreamSource<Integer> ds= env.fromCollection(lists) ;
        //DataStreamSource<Integer> ds = env.fromElements(1, 2, 3, 4, 5);

        //从socket读取数据
        //DataStreamSource<String> ds = env.socketTextStream("hadoop102", 8888);

        //数据生成器
        DataGeneratorSource<String> ds = new DataGeneratorSource<>(
                new GeneratorFunction<Long, String>() {
                    @Override
                    public String map(Long value) throws Exception {
                        return UUID.randomUUID().toString() + " -> " + value;
                    }
                }
                ,
                100
                ,
                RateLimiterStrategy.perSecond(1)
                ,
                Types.STRING
        );

        env.fromSource(ds , WatermarkStrategy.noWatermarks() , "datagen" )
                .print();

        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

















